package city.spring.modules.oldman.service.impl;

import city.spring.modules.oldman.entity.OldmanComplaintEntity;
import city.spring.modules.oldman.entity.OldmanInfoEntity;
import city.spring.modules.oldman.repository.ComplaintReceiverExtRepository;
import city.spring.modules.oldman.repository.OldmanComplaintRepository;
import city.spring.modules.oldman.service.OldmanComplaintService;
import city.spring.modules.oldman.service.OldmanInfoService;
import city.spring.modules.oldman.vo.OldmanComplaintVO;
import city.spring.modules.personnel.entity.EmployeeEntity;
import city.spring.modules.personnel.service.EmployeeService;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：投诉
 *
 * @author pxm
 * @date 2020-06-22 10:43:20
 */
@CacheConfig(cacheNames = {OldmanComplaintServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class OldmanComplaintServiceImpl extends ServiceImpl<OldmanComplaintRepository, OldmanComplaintEntity> implements OldmanComplaintService {

    private final UserService userService;
    private final OldmanInfoService oldmanInfoService;
    private final OldmanComplaintRepository oldmanComplaintRepository;
    private final ComplaintReceiverExtRepository complaintReceiverExtRepository;
    private final EmployeeService employeeService;

    public OldmanComplaintServiceImpl(UserService userService, OldmanInfoService oldmanInfoService, OldmanComplaintRepository oldmanComplaintRepository, ComplaintReceiverExtRepository complaintReceiverExtRepository, EmployeeService employeeService) {
        this.userService = userService;
        this.oldmanInfoService = oldmanInfoService;
        this.oldmanComplaintRepository = oldmanComplaintRepository;
        this.complaintReceiverExtRepository = complaintReceiverExtRepository;
        this.employeeService = employeeService;
    }

    @Override
    public void saveOldmanComplaint(OldmanComplaintEntity entity) {
        // 设置联系老人的身份证号、手机号、受理机构
        OldmanInfoEntity oldmanInfoEntity = oldmanInfoService.getById(entity.getOldman());
        entity.setOrg("白云颐养中心");
        entity.setHandle(0);
        entity.setReview(0);
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateOldmanComplaint(OldmanComplaintEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteOldmanComplaint(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteOldmanComplaint(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public Object updateHandle(String id, OldmanComplaintEntity entity) {
        if (entity.getHandle().equals(0)) {
            throw new RuntimeException("该条记录处理状态为已处理！");
        }
        if (entity.getHandle().equals(0)) {
            entity.setHandle(1);
        }
        oldmanComplaintRepository.updateHandle(id, entity);
        return null;
    }

    @Override
    public Object updateReview(String id, OldmanComplaintEntity entity) {
        if (entity.getReview().equals(1)) {
            throw new RuntimeException("该条记录回访状态为已回访！");
        }
        if (entity.getReview().equals(0)) {
            entity.setReview(1);
        }
        oldmanComplaintRepository.updateReview(id, entity);
        return null;
    }

    @Override
    public OldmanComplaintVO getComplaint(String id) {
        OldmanComplaintEntity complaintEntity = getById(id);
        OldmanComplaintVO complaintVO = new OldmanComplaintVO(complaintEntity);
        // 老人姓名
        OldmanInfoEntity oldmanInfoEntity = oldmanInfoService.getById(complaintEntity.getOldman());
        complaintVO.setOldmanName(oldmanInfoEntity.getOldmanName());
        // 证件号
        complaintVO.setCertificate(oldmanInfoEntity.getCertificate());
        // 手机号码
        complaintVO.setTelephone(oldmanInfoEntity.getTelephone());
        // 回访人姓名
        if (!(complaintVO.getReviewer() == null)) {
            EmployeeEntity employeeEntity = employeeService.getById(complaintEntity.getReviewer());
            complaintVO.setReviewerName(employeeEntity.getName());
        }
        //处理状态
        switch (complaintVO.getHandle()) {
            case 0:
                complaintVO.setHandleState("待处理");
                break;
            case 1:
                complaintVO.setHandleState("在处理");
                break;
            case 2:
                complaintVO.setHandleState("已处理");
                break;
            default:
        }
        //回访状态
        switch (complaintVO.getReview()) {
            case 0:
                complaintVO.setReviewState("待回访");
                break;
            case 1:
                complaintVO.setReviewState("已回访");
                break;
            case 2:
                complaintVO.setReviewState("回访不成功");
                break;
            default:
        }
        // 加载投诉的受理人员列表
        complaintVO.setReceivers(complaintReceiverExtRepository.getComplaintReceivers(id));

//        List receiverList = complaintVO.getReceivers();
//
//        for(int i = 0 ; i < receiverList.size() ; i++) {
//            String receiver = (String) receiverList.get(4);
//            EmployeeEntity employeeEntity = employeeService.getById(receiver);
//        }

        return complaintVO;
    }

    @Override
    public IPage<OldmanComplaintVO> listPage(Page<? extends OldmanComplaintEntity> page, Wrapper<? extends OldmanComplaintEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

}